<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>级联选择演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        select, button {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            cursor: pointer;
            margin-top: 20px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #e9ecef;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>级联选择演示</h1>
        <div class="form-group">
            <label for="province">选择省份:</label>
            <select id="province" onchange="updateCities()">
                <option value="">请选择省份</option>
            </select>
        </div>
        <div class="form-group">
            <label for="city">选择城市:</label>
            <select id="city">
                <option value="">请先选择省份</option>
            </select>
        </div>
        <button onclick="validateSelection()">验证选择</button>
        <div id="result" class="result" style="display: none;"></div>
    </div>

    <script>
        // 省份和城市数据
        const provinceCityData = {
            "北京市": ["东城区", "西城区", "朝阳区", "丰台区", "石景山区"],
            "上海市": ["黄浦区", "徐汇区", "长宁区", "静安区", "普陀区"],
            "广东省": ["广州市", "深圳市", "珠海市", "汕头市", "佛山市"],
            "江苏省": ["南京市", "苏州市", "无锡市", "常州市", "镇江市"],
            "浙江省": ["杭州市", "宁波市", "温州市", "嘉兴市", "湖州市"],
            "四川省": ["成都市", "绵阳市", "自贡市", "攀枝花市", "泸州市"]
        };

        // 初始化省份选项
        function initProvinces() {
            const provinceSelect = document.getElementById("province");
            // 保留第一个选项
            provinceSelect.innerHTML = '<option value="">请选择省份</option>';
            
            // 添加省份选项
            for (const province in provinceCityData) {
                const option = document.createElement("option");
                option.value = province;
                option.textContent = province;
                provinceSelect.appendChild(option);
            }
        }

        // 更新城市选项
        function updateCities() {
            const provinceSelect = document.getElementById("province");
            const citySelect = document.getElementById("city");
            const selectedProvince = provinceSelect.value;

            // 清空城市选项
            citySelect.innerHTML = '<option value="">请选择城市</option>';

            if (selectedProvince && provinceCityData[selectedProvince]) {
                // 添加城市选项
                provinceCityData[selectedProvince].forEach(city => {
                    const option = document.createElement("option");
                    option.value = city;
                    option.textContent = city;
                    citySelect.appendChild(option);
                });
                citySelect.disabled = false;
            } else {
                citySelect.disabled = true;
            }
        }

        // 验证选择
        function validateSelection() {
            const provinceSelect = document.getElementById("province");
            const citySelect = document.getElementById("city");
            const resultDiv = document.getElementById("result");
            
            resultDiv.style.display = "block";
            
            if (!provinceSelect.value) {
                alert("请选择省份！");
                resultDiv.style.display = "none";
                return;
            }
            
            if (!citySelect.value) {
                alert("请选择城市！");
                resultDiv.style.display = "none";
                return;
            }
            
            resultDiv.innerHTML = `
                <h3>选择结果：</h3>
                <p>省份：${provinceSelect.value}</p>
                <p>城市：${citySelect.value}</p>
                <p style="color: green;">选择完成！</p>
            `;
        }

        // 页面加载完成后初始化
        window.onload = function() {
            initProvinces();
        };
    </script>
</body>
</html>